﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GtsServer.Service.Model;
using GtsServer.Service.Model.Response;
using GtsServer.Model;
using GtsServer.DAL;
using System.Data;
using GtsServer.Common;


namespace GtsServer.Service.Controller
{
    public class VersionController : BaseController
    {
        InitInfo info = new InitInfo();
        int goldNum = 0;
        string userAccount = string.Empty;

        public VersionController() { }

        public VersionController(InitInfo model, out int goldNum, out string userAccount)
            : base(model, out goldNum, out userAccount)
        {
            this.goldNum = goldNum;
            this.userAccount = userAccount;
            info = model;
        }

        public VersionResponse GetVersion(int goldNum, string userAccount)
        {
            VersionResponse response = new VersionResponse();
            response.version = new VersionManage();
            response.notice = new NoticeManage();
            response.user = new InitUserManage();
            response.user.sys_account = userAccount;

            UserPrizeDAL updal = new UserPrizeDAL();
            DataSet ds = new DataSet();
            if (goldNum == 0)
            {
                ds = updal.GetInitPrizeGold(userAccount);
                goldNum = Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString());
            }

            response.coin = goldNum;
            //调用数据库  返回数据
            VersionDAL versionDal = new VersionDAL();
            NoticeDAL noticedal = new NoticeDAL();
            VersionManage versionManage = new VersionManage();
            ds = versionDal.GetListReturn(" v.num>" + info.version.code + " and c.pid='" + info.phone.pid + "'");
            if (ds.Tables[0].Rows.Count > 0)
            {
                VersionModel plVersionModel = IListDataSet.DataSetToIList<VersionModel>(ds, 0).First();

                versionManage = new VersionManage()
                {
                    force = plVersionModel.IsCompel.Value ? Convert.ToInt32(EnumVersionIscompel.强制更新) : Convert.ToInt32(EnumVersionIscompel.正常更新),
                    content = plVersionModel.Remark,
                    url = CommonConfig.imgServiceAddr + CommonConfig.version + plVersionModel.Address
                };
            }
            else
            {
                versionManage = new VersionManage()
                {
                    force = Convert.ToInt32(EnumVersionIscompel.不更新),
                    content = "",
                    url = ""
                };
            }
            response.version = versionManage;

            ds.Clear();
            ds = noticedal.GetListReturn("IsFullScre=1 and StartTime <='" + DateTime.Now + "' and EndTime>='" + DateTime.Now + "' order by CreateTime desc ");

            if (ds.Tables[0].Rows.Count > 0)
            {
                Notice plNoticeModel = IListDataSet.DataSetToIList<Notice>(ds, 0).First();

                NoticeManage noticeManage = new NoticeManage()
                {
                    show = Int32.Parse(plNoticeModel.State.ToString()),
                    content = plNoticeModel.NoticeContent
                };
                response.notice = noticeManage;
            }

            return response;
        }
    }
}
